//
//  Hear_AddPhotoViewController.m
//  Hear
//
//  Created by mac on 2018/4/27.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "Hear_AddPhotoViewController.h"

#import "OSSImageUploader.h"

@interface Hear_AddPhotoViewController () <LQPhotoPickerViewDelegate>


@end

@implementation Hear_AddPhotoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"留声机配图";
    
    UIButton *savebtn = [[UIButton alloc]init];
    savebtn.size = CGSizeMake(30, 25);
    [savebtn setTitle:@"保存" forState:UIControlStateNormal];
    savebtn.titleLabel.font = CustomUIFont(14);
    [savebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [savebtn addTarget:self action:@selector(savebtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:savebtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.LQPhotoPicker_superView = self.view;
    
    self.LQPhotoPicker_imgMaxCount = 9;
    
    [self LQPhotoPicker_initPickerView];
    
    self.LQPhotoPicker_delegate = self;
    
}


- (void)savebtnAction:(UIButton *)sender {
    
    sender.enabled = NO;
    
    if (self.LQPhotoPicker_selectedAssetArray.count){
        // 有图片先上传图片
        [OSSImageUploader asyncUploadImages:self.LQPhotoPicker_selectedAssetArray complete:^(NSArray<NSString *> *names, UploadImageState state) {
            sender.enabled = YES;
            
            if (state == UploadImageFailed) return ;
            
            NSMutableArray *formatImageArr = [NSMutableArray array];
            
            for (NSString *url in names) {
                [formatImageArr addObject:[NSString stringWithFormat:@"http://%@.%@%@",BucketName,AliYunHost,url]];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                NSError *error = nil;
                NSString *imageJson = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:formatImageArr options:NSJSONWritingPrettyPrinted error:&error] encoding:NSUTF8StringEncoding];
                if (self.getImageStr) {
                    self.getImageStr(imageJson);
                    [self showMessage:@"上传成功" atPosition:ShowPosition_Center];
                    [self.navigationController popViewControllerAnimated:YES];
                }
            });
        }];
        return;
    } else {
        [self showMessage:@"请先选择图片" atPosition:ShowPosition_Center];
    }
    
    sender.enabled = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

